const fs=require('fs');
const readFile = require("util").promisify(fs.readFile);
const stat = require('util').promisify(fs.stat)
const parseurl = require("parseurl");
const path = require('path');
const root = process.cwd();

async function readSource(req){
    const {pathname} =parseurl(req);
    const filepath = path.resolve(root,pathname.replace(/^\//,''))
    console.log(filepath,pathname,root,'---')

    return {
        filepath,
        source:await readFile(filepath,'utf-8'),
        updateTime:(await stat(filepath)).mtime.getTime()
    }
}

exports.readSource = readSource